import requests
import json
import pandas as pd
from PIL import Image
from io import BytesIO

#请在高德平台上获取Key,在address输入地址
#必填参数：key,address  选填参数：city，batch，sig，output，callback
#具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/georegeo#geo
def geocode(key,address,city=None,batch=None,sig=None,callback=None):
    """获取高德API的地理编码"""
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address,
        "city":city,
        "batch":batch,
        "sig":sig,
        "callback":callback,
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0][ 'location']
    return results

#请在高德平台上获取Key,在location输入经纬度坐标
#必填参数：key,location  选填参数：poitype,radius,extensions,batch,output,homeorcorp,roadlevel,sig,callback
#具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/georegeo#regeo
def regeo(key,location,poitype=None,radius=1000,extensions="base",batch=None,output=None,homeorcorp=None,roadlevel=None,sig=None,callback=None):
    """获取高德API的逆地理编码"""
    regeo_url = "https://restapi.amap.com/v3/geocode/regeo"
    params = {
        "key":key,
        "location":location,
        "poitype":poitype,
        "radius":radius,
        "extensions":extensions,
        "batch":batch,
        "output": "json",
        "homeorcorp": homeorcorp,
        "roadlevel":roadlevel,
        "sig":sig,
        "callback":callback,
    }
    r = requests.get(regeo_url,params=params)
    results = r.json()[ 'regeocode'][ 'formatted_address']
    return results

#> 请在高德平台上获取Key,可使用geocode函数获取origin和destination的具体经纬度坐标
#必填参数：key，origin，destination  选填参数：sig，output，callback
#具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/direction#walk
def walking(key,origin,destination,sig=None,output=None,callback=None):
    """获取高德API的步行路径"""
    walking_url = "https://restapi.amap.com/v3/direction/walking"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
         "output":"json",
        "sig":sig,
        "callback":callback
    }
    r=requests.get(walking_url,params=params)
    results = r.json()['route'][ 'paths'][0]['steps']
    return results

#请在高德平台上获取Key,可使用geocode函数获取origin和destination的具体经纬度坐标，city为城市/跨城规划时的起点城市
#必填参数：key，origin，destination，city  选填参数：cityd，extensions，strategy，nightflag，date，time，sig，output，callback
#具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/direction#bus
def integrated(key,origin,destination,city,cityd=None,extensions=None,strategy=None,nightflag=None,date=None,time=None,output=None,callback=None):
    """获取高德API的公交车路径"""
    url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city,
        "cityd":cityd,
        "extensions":extensions,
        "strategy":strategy,
        "nightflag":nightflag,
        "date":date,
        "time":time,
        "output":"json",
        "callback":callback
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'route']['transits'][0][ 'segments'][0]['bus']['buslines'][0][ 'via_stops'][0]
    return results

#请在高德平台上获取Key,可使用geocode函数获取origin和destination的具体经纬度坐标，city为城市/跨城规划时的起点城市
#必填参数：key，origin，destination
#选填参数:originid,destinationid,origintype,destinationtype,strategy,waypoints,avoidpolygons,avoidroad,province,number,cartype,ferry,roadaggregation,nosteps,sig，output，callback,extensions
#具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/direction#driving
def driving(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=None,waypoints=None,avoidpolygons=None,avoidroad=None,province=None,number=None,cartype=0,ferry=0,roadaggregation=None,nosteps=None,sig=None,output=None,callback=None,extensions=None):
    """获取高德API的驾车路径"""
    url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "originid":originid,
        "destinationid":destinationid,
        "origintype":origintype,
        "destinationtype":destinationtype,
        "strategy":strategy,
        "waypoints":waypoints,
        "avoidpolygons":avoidpolygons,
        "avoidroad":avoidroad,
        "province":province,
        "number":number,
        "cartype":cartype,
        "ferry":ferry,
        "roadaggregation":roadaggregation,
        "nosteps":nosteps,
        "sig":sig,
        "output":json,
        "callback":callback,
        "extensions":extensions
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'route']['paths'][0][ 'steps']
    return results

#1、请在高德平台上获取Key,可使用geocode函数获取origin和destination的具体经纬度坐标
#2、必填参数：key，origin，destination
#3、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/direction#t8
def bicycling(key,origin,destination):
    """获取高德API的骑行路径"""
    url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    response = requests.get(url,params=params)
    results = response.json()['data'][ 'paths'][0][ 'steps']
    return results

#> 1、请在高德平台上获取Key,可使用geocode函数获取origin和destination的具体经纬度坐标
#2、必填参数：key，origin，destination，size
#3、选填参数:originid,origintype,destinationid,destinationtype,diu,strategy,waypoints,height,width,load,weight,axis,province,number,cartype,avoidpolygons,showpolyline,nosteps
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/direction#t9
def truck(key,origin,destination,size=2,originid=None,originidtype=None,destinationid=None,destinationtype=None,diu=None,strategy=1,waypoints=None,height=1.6,width=2.5,load=10,weight=0.9,axis=2,province=None,number=None,cartype=0,avoidpolygons=None,showpolyline=1,nosteps=0):
    """获取高德API的货车路径"""
    url = "https://restapi.amap.com/v4/direction/truck?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "size":size,
        "originid":originid,
        "originidtype":originidtype,
        "destinationid":destinationid,
        "destinationtype":destinationtype,
        "diu":diu,
        "strategy":strategy,
        "waypoints":waypoints,
        "height":height,
        "width":width,
        "load":load,
        "weight":weight,
        "axis":axis,
        "province":province,
        "number":number,
        "cartype":cartype,
        "avoidpolygons":avoidpolygons,
        "showpolyline":showpolyline,
        "nosteps":nosteps,
    }
    response = requests.get(url,params=params)
    results = response.json()['data']['route'][ 'paths'][0][ 'steps']
    return results

# 1、请在高德平台上获取Key,可使用geocode函数获取origin和destination的具体经纬度坐标
#2、必填参数：key，origin，destination
#3、选填参数:type,sig，output，callback
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/direction#distance
def distance(key,origins,destination,type=1,sig=None,output=None,callback=None):
    """获取高德API的距离测量功能"""
    url = "https://restapi.amap.com/v3/distance?parameters"
    params = {
        "key":key,
        "origins":origins,
        "destination":destination,
        "type":type,
         "sig":sig,
         "output":json,
        "callback":callback
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'results'][0]['distance']
    return results

#> 1、请在高德平台上获取Key
#2、必填参数：key
#3、选填参数:keywords,subdistrict,page,offset,extensions,filter,callback,output
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/district
def district(key,keywords=None,subdistrict=None,page=None,offset=None,extensions=None,filter=None,callback=None,output=None):
    """获取高德API的行政区域查询"""
    district_url = "https://restapi.amap.com/v3/config/district"
    params = {"key":key,
              "output":"json",
              "keywords":keywords,
              "subdistrict":subdistrict,
              "page":page,
              "offset":offset,
              "extensions":extensions,
              "filter":filter,
              "callback":callback,
    }
    r = requests.get(district_url,params=params)
    results = r.json()[ 'districts'][0][ 'districts']
    return results

#> 1、请在高德平台上获取Key
#2、必填参数：key,keywords或types
#3、选填参数:city,citylimit,children,offset,page,extensions,sig,output,callback
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/search
def text(key,keywords=None,types=None,city=None,citylimit=None,children=None,offset=None,page=None,extensions=None,sig=None,output=None,callback=None):
    """获取高德API的搜索POI"""
    text_url = "https://restapi.amap.com/v3/place/text"
    params = {
        "key":key,
        "keywords":keywords,
        "city":"110000",
        "output":"json",
        "types":types,
        "citylimit":citylimit,
        "children":children,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "callback":callback,
    }
    r = requests.get(text_url,params=params)
    results = r.json()[ 'pois']
    return results

#> 请在高德平台上获取Key
#必填参数：key
#选填参数:ip,sig,output
#具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/ipconfig
def ip(key,ip=None,sig=None,output=None):
    """获取高德API的IP定位"""
    ip_url = "https://restapi.amap.com/v3/ip"
    params = {
        "key":key,
        "output":json,
        "sig":sig
    }
    r = requests.get(ip_url,params=params)
    results = r.json()[ 'rectangle']
    return results

#> 1、请在高德平台上获取Key，可使用geocode函数获取location的具体经纬度坐标,zoom范围为1-17
#2、必填参数：key，location，zoom
#3、选填参数:size,scale,markers,label,paths,traffic,sig
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/staticmaps
def staticmap(key,location,zoom=None,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    """获取高德API的静态地图"""
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key":key,
        "location":location,
        "zoom":zoom,
        "size":size,
        "scale":scale,
        "markers":markers,
        "labels":labels,
        "paths":paths,
        "traffic":traffic,
        "sig":sig
    }
    response = requests.get(url,params=params)
    results = Image.open(BytesIO(response.content))
    return results


#> 1、请在高德平台上获取Key，locations为其他导航软件的具体经纬度坐标
#2、必填参数：key，locations
#3、选填参数:coordsys,sig,output
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/convert
def convert(key,locations,coordsys=None,sig=None,output=None):
    """将其他导航地理编码转换为高德地理编码"""
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":key,
        "locations":locations,
        "coordsys":coordsys,
        "sig":sig,
        "output":output
    }
    response = requests.get(url,params=params)
    result = response.json()[ 'locations']
    return result

#> 1、请在高德平台上获取Key，city为城市编码，具体信息可参考城市编码表:https://lbs.amap.com/api/webservice/download
#2、必填参数：key，city
#3、选填参数:extensions,output
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/weatherinfo
def weatherInfo(key,city,extensions=None,output=None):
    """获取高德API的天气查询功能"""
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key,
        "city":city,
        "extensions":extensions,
        "output":json
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'lives'][0]
    return results

#> 1、请在高德平台上获取Key，
#2、必填参数：key，keywords
#3、选填参数:type,location,city,citylimit,datatype,sig,output,callback
#4、具体输入格式请参照高德API文档：https://lbs.amap.com/api/webservice/guide/api/inputtips
def inputtips(key,keywords,type=None,location=None,city=None,citylimit=None,datatype=None,sig=None,output=None,callback=None):
    """获取高德API的输入提示功能"""
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "type":type,
        "location":location,
        "city":city,
        "citylimit":citylimit,
        "datatype":datatype,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'tips']
    return results
